/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ObatDialog.java
 *
 * Created on 12 Jun 12, 15:14:28
 */
package Com.Interface;

import Com.Controller.MainController;
import Com.Entity.Koneksi;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author Ardha
 */
public class ObatDialog extends javax.swing.JDialog {

    /** Creates new form ObatDialog */
    public ObatDialog(java.awt.Frame parent, boolean modal, JTextField idPasien) {
        super(parent, modal);
        initComponents();
        try {
            showIDLabel.setText(idPasien.getText());
            addObatCombo(showIDLabel.getText());
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ObatDialog.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ObatDialog.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ObatDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        tahapLabel = new javax.swing.JLabel();
        idPasienLabel = new javax.swing.JLabel();
        obatLabel = new javax.swing.JLabel();
        showIDLabel = new javax.swing.JLabel();
        showTahapLabel = new javax.swing.JLabel();
        obatCombo = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        dosisLabel = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        showKategoriLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        ketArea = new javax.swing.JTextArea();
        showNamaObatLabel = new javax.swing.JLabel();
        showDosisLabel = new javax.swing.JLabel();
        lanjutButton = new javax.swing.JButton();
        kembaliButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        tahapLabel.setText("Tahap");

        idPasienLabel.setText("ID Pasien");

        obatLabel.setText("Obat");

        obatCombo.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N
        obatCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "-Pilih Obat-" }));
        obatCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                obatComboActionPerformed(evt);
            }
        });

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Keterangan Obat", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N

        jLabel1.setText("Kategori");

        jLabel2.setText("Nama Obat");

        dosisLabel.setText("Dosis");

        jLabel4.setText("Keterangan");

        showKategoriLabel.setText(" ");

        ketArea.setColumns(20);
        ketArea.setEditable(false);
        ketArea.setLineWrap(true);
        ketArea.setRows(5);
        jScrollPane1.setViewportView(ketArea);

        showNamaObatLabel.setText(" ");

        showDosisLabel.setText(" ");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(32, 32, 32)
                        .addComponent(showKategoriLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 284, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(dosisLabel)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
                            .addComponent(showDosisLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE)
                            .addComponent(showNamaObatLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 282, Short.MAX_VALUE))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(showKategoriLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(showNamaObatLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dosisLabel)
                    .addComponent(showDosisLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE))
                .addContainerGap())
        );

        lanjutButton.setText("Lanjut");
        lanjutButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lanjutButtonActionPerformed(evt);
            }
        });

        kembaliButton.setText("Kembali");
        kembaliButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                kembaliButtonActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Arial", 0, 14)); // NOI18N
        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel3.setText("OBAT UNTUK PASIEN");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(114, 114, 114)
                        .addComponent(lanjutButton)
                        .addGap(45, 45, 45)
                        .addComponent(kembaliButton))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(idPasienLabel)
                                    .addComponent(tahapLabel)
                                    .addComponent(obatLabel))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(showIDLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE)
                                    .addComponent(showTahapLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE)
                                    .addComponent(obatCombo, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addContainerGap())
            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(idPasienLabel)
                    .addComponent(showIDLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tahapLabel)
                    .addComponent(showTahapLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(obatLabel)
                    .addComponent(obatCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lanjutButton)
                    .addComponent(kembaliButton))
                .addGap(21, 21, 21))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void obatComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_obatComboActionPerformed
        // TODO add your handling code here:
        System.out.println(obatCombo.getSelectedIndex());
        if (obatCombo.getSelectedIndex() != 0) {
            cek = 0;
            try {
                String query = "SELECT kategori.Kode_Kategori, obat.Nama_Obat, "
                        + "UPPER(golongan.Tahap_Minum), golongan.Dosis, obat.Kandungan_Obat "
                        + "FROM kategori, obat, golongan "
                        + "WHERE kategori.Kode_Kategori = golongan.Kode_Kategori "
                        + "AND obat.Kode_Obat = golongan.Kode_Obat "
                        + "AND UPPER(obat.Nama_Obat) = '"
                        + String.valueOf(obatCombo.getSelectedItem()).toUpperCase()
                        + "' AND golongan.Kode_Kategori = "
                        + "(SELECT Kode_Kategori FROM rekam_medis "
                        + "WHERE UPPER(Id_Pasien)='" + showIDLabel.getText() + "' "
                        + "AND No_Rekam_Medis = "
                        + "(SELECT MAX(No_Rekam_Medis) FROM rekam_medis "
                        + "WHERE UPPER(Id_Pasien)='" + showIDLabel.getText() + "'))";
                Statement stmt = Koneksi.getDBConnection().createStatement();
                ResultSet rs = stmt.executeQuery(query);
                String kode = null;
                String namaObat = null;
                String tahap = null;
                String dosis = null;
                String kandungan = null;
                if (rs.next()) {
                    kode = rs.getString(1);
                    namaObat = rs.getString(2);
                    tahap = rs.getString(3);
                    dosis = rs.getString(4);
                    kandungan = rs.getString(5);
                }
                showKategoriLabel.setText(kode);
                showNamaObatLabel.setText(namaObat);
                showTahapLabel.setText(tahap);
                showDosisLabel.setText(dosis);
                String bln = dosis.substring(0, 1);
                bulan = Integer.parseInt(bln);
                nObat = namaObat;
                ketArea.setText(kandungan);
            } catch (SQLException ex) {
                Logger.getLogger(ObatDialog.class.getName()).log(Level.SEVERE, null, ex);
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ObatDialog.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(ObatDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            cek = 1;
        }
    }//GEN-LAST:event_obatComboActionPerformed

    private void lanjutButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lanjutButtonActionPerformed
        // TODO add your handling code here:
        if (cek == 0) {
            MainController.setCobaCobi(nObat, bulan);
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(this, "Obat belum dipilih");
        }
    }//GEN-LAST:event_lanjutButtonActionPerformed

    private void kembaliButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_kembaliButtonActionPerformed
        try {
            // TODO add your handling code here:
            int id = 0;
            MainController.setNtfObat(0);
            String q1 = "SELECT MAX(No_Rekam_Medis) FROM rekam_medis "
                    + "WHERE UPPER(Id_Pasien)='" + showIDLabel.getText().toUpperCase() + "'";
            Statement stmt = Koneksi.getDBConnection().createStatement();
            ResultSet rs = stmt.executeQuery(q1);
            if (rs.first()) {
                id = rs.getInt(1);
            }
            String q2 = "DELETE FROM rekam_medis WHERE No_Rekam_Medis =" + id;
            stmt.executeUpdate(q2);
            this.dispose();
        } catch (SQLException ex) {
            Logger.getLogger(ObatDialog.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ObatDialog.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ObatDialog.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_kembaliButtonActionPerformed

    private void addObatCombo(String idPasien) throws FileNotFoundException, IOException, SQLException {
        String query = "SELECT obat.Nama_Obat "
                + "FROM golongan,obat "
                + "WHERE golongan.Kode_Kategori = "
                + "(SELECT Kode_Kategori FROM rekam_medis "
                + "WHERE UPPER(Id_Pasien)='" + idPasien + "' "
                + "AND No_Rekam_Medis = "
                + "(SELECT MAX(No_Rekam_Medis) FROM rekam_medis "
                + "WHERE UPPER(Id_Pasien)='" + idPasien + "')) "
                + "AND UPPER(Tahap_Minum) = "
                + "(SELECT Tahap_priksa FROM rekam_medis "
                + "WHERE UPPER(Id_Pasien)='" + idPasien + "' "
                + "AND No_Rekam_Medis = "
                + "(SELECT MAX(No_Rekam_Medis) FROM rekam_medis "
                + "WHERE UPPER(Id_Pasien)='" + idPasien + "')) "
                + "AND golongan.Kode_Obat=obat.Kode_Obat";
        Statement stmt = Koneksi.getDBConnection().createStatement();
        ResultSet rset = stmt.executeQuery(query);
        while (rset.next()) {
            obatCombo.addItem(rset.getString(1));
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel dosisLabel;
    private javax.swing.JLabel idPasienLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton kembaliButton;
    private javax.swing.JTextArea ketArea;
    private javax.swing.JButton lanjutButton;
    private javax.swing.JComboBox obatCombo;
    private javax.swing.JLabel obatLabel;
    private javax.swing.JLabel showDosisLabel;
    private javax.swing.JLabel showIDLabel;
    private javax.swing.JLabel showKategoriLabel;
    private javax.swing.JLabel showNamaObatLabel;
    private javax.swing.JLabel showTahapLabel;
    private javax.swing.JLabel tahapLabel;
    // End of variables declaration//GEN-END:variables
    int bulan;
    String nObat;
    int cek = 0;
}
